<?php
/**
 +------------------------------------------------------------------------------
 * Money资产操作
 +------------------------------------------------------------------------------
 * @category   SubAction
 * @package  app
 * @subpackage  Action
 * @author   朝闻道 <admin@iic.cn>
 * @date 2013-5-17
 * @time  下午3:38:07
 +------------------------------------------------------------------------------
 */
class Money {
	protected $line = '';
	protected $isdebug = '';
	/**
	 * 货币转换成优惠劵
	 * */
	function tocoupon($userid = 0, $subject = '', $value = 0, $lv = array(), $opt = array()) {
		if($value>0){
			$value = $value * 100;
		}
		// 货币转换成优惠劵
		$this->line = 'recharge::' . __LINE__;
		if (empty ( $userid )) {
			self::_err ( '用户ID不能为空。' );
		}
		if (empty ( $subject )) {
			self::_err ( '参数错误。' );
		}
		if (empty ( $value ) || $value < 0) {
			self::_err ( '变动数额不能为0' );
		}
		if (empty ( $opt ['changtype'] )) {
			self::_err ( '变动类型必须填写' );
		}
		if (($opt ['changtype'] == 'ADMIN_ADD' || $opt ['changtype'] == 'ORDER_BAK' || $opt ['changtype'] == 'ADMIN_DECR') && empty ( $opt ['action_userid'] )) {
			self::_err ( '充值、退款操作时操作人ID不能为空。' );
		}
		$opt ['action_userid'] = isset ( $opt ['action_userid'] ) ? $opt ['action_userid'] : $userid;
		$assetc = array (
				'platform' => 'SMW01' 
		);
		$assetc ['changtype'] = $opt ['changtype'];
		$assetc ['action_userid'] = $opt ['action_userid'];
		$assetc ['cip'] = $opt ['cip'];
		$assetc ['bzvu'] = $opt ['bzvu'];
		$assetc ['userid'] = $userid;
		$assetc ['subject'] = $subject;
		$assetc ['value'] = '-'.$value;
		$assetc ['ctime'] = time ();
		// 获取用户当前的余额
		$money = self::money_info ( $userid, 'SMW01', $subject );
		
		// 写入用户余额
		if (empty ( $money ) && $money !== false) {
			$money = 0;
		}
		$money = $money - $value;
		
		// $wres=self::_w_money($userid,$money,'SMW01',$subject);//启用事件回滚的需要暂时不调用此函数
		$where = array ();
		$where ['userid'] = $userid;
		$where ['subject'] = $subject;
		$where ['platform'] = 'SMW01';
		$data = $where;
		$usermoney = M ( 'UserMoney' );
		$res = $usermoney->where ( $where )->find ();
		$usermoney->startTrans ();
		$data ['value'] = $money;
		$data ['mtime'] = time ();
		if ($res) {
			$data ['id'] = $res ['id'];
			$return = $usermoney->save ( $data );
		} else {
			$return = $usermoney->add ( $data );
		}
		if (empty ( $return )) {
			self::_err ( '账号操作错误。' );
		}
		$assetc ['nowvalue'] = $money;
		
		//写入优惠劵表
		
		$coupon=M("UserCoupon");
		$coupon_data=array();
		$time=time();
		$coupon_data['point']=$value;
		$cv = $lv[$value/100];
		$coupon_data['value']=$cv*100;
		$coupon_data['type']=1;
		$coupon_data['createtime']=$time;
		$coupon_data['usetime']=0;
		$coupon_data['cip']=get_client_ip(1);
		$coupon_data['useip']=0;
		$coupon_data['status']=0;
		$coupon_data['order_sn']=0;
		$coupon_data['starttime']=$time;
		$coupon_data['endtime']=$time+60*60*24*365;
		$coupon_data['userid']=$userid;
		$coupon->startTrans ();
		$addid=$coupon->add($coupon_data);
		
		// 写入充值流水
		$assetc_dao = M ( "UserAssets" );
		$assetc_dao->startTrans ();
		$assetc_id = $assetc_dao->add ( $assetc );
		if ($assetc_id) {
			$usermoney->commit ();
			$assetc_dao->commit ();
			$coupon->commit ();
			return true;
		} else {
			$usermoney->rollback ();
			$assetc_dao->rollback ();
			$coupon->rollback ();
			return false;
		}
	} // end recharge
	
	/**
	 * 扣云购商的MV
	 * @param number $userid 受理人
	 * @param number $value 扣MV的数额
	 * @param number $orderid 相关订单号
	 * @param number $adminid 操作人
	 * @return number|Ambigous <当前余额MV, boolean 扣款不成功, number CV不足>
	 */
	function recharge_mv($userid = 0, $value = 0, $orderid=0, $adminid=0){
		$ret = self::recharge($userid,'MV',$value,array(
	        	'action_userid'=>$adminid,
	         	'cip'=>get_client_ip(),
	        	'bzvu'=>'触发冻结MV订单：'.$orderid,
	         	'changtype'=>'O2oOrderAcceptFreeze',
	         	'oper' => '-'
	         	),true);
		if($ret == -1){
			$money = self::money_info ( $userid, 'SMW01', 'MV' );
			$temp_value = ($value*100-$money)/1000;
			$ret = self::recharge($userid,'CV',$temp_value,array(
	        	'action_userid'=>$adminid,
	         	'cip'=>get_client_ip(),
	        	'bzvu'=>'消费CV转MV订单：'.$orderid,
	         	'changtype'=>'CVTOMV',
	         	'oper' => '-'
	         	),true);
			if($ret == -1){
				//CV不足抵扣MV
				return -1;
			}else{
				if($ret !== false){
					$ret = self::recharge($userid,'MV',$temp_value*10,array(
			        	'action_userid'=>$adminid,
			         	'cip'=>get_client_ip(),
			        	'bzvu'=>'MV充值订单：'.$orderid,
			         	'changtype'=>'CVTOMV',
			         	));
					if($ret !== false){
						$ret = $this->recharge_mv($userid,$value,$orderid,$adminid);
					}
				}
			}
		}
		return $ret;
	}
	/**
	 * SMW01平台用户充值
	 * 
	 * @param int $userid
	 *        	用户ID
	 * @param string $subject
	 *        	财务科目 CV BV MV PV
	 * @param int $value
	 *        	充值数额 分为单位
	 * @param array $opt=array(
	 *        	'action_userid'=>'操作人',
	 *        	'cip'=>'IP',
	 *        	'bzvu'=>'订单号，授权码，操作原因',
	 *        	'changtype'=>'操作类型 ADMIN_ADD,ORDER_USE,ORDER_BAK,PV_CHANG,MV_CHANG,TENPAY_ADD,CVTOMV,O2oOrderAcceptFreeze,ADMIN_DECR',
	 * 			'oper' => '-'
	 *        	)
	 * @return 当前余额 或 false
	 *         @date 2013-5-17
	 *         @time 下午3:40:06
	 */
	function recharge($userid = 0, $subject = '', $value = 0, $opt = array(), $isRet = false) {
		if($value>0){
			$value = $value * 100;
		}
		// 用户充值
		$this->line = 'recharge::' . __LINE__;
		if (empty ( $userid )) {
			self::_err ( '用户ID不能为空。' );
		}
		if (empty ( $subject )) {
			self::_err ( '参数错误。' );
		}
		if (empty ( $value ) || $value < 0) {
			self::_err ( '变动数额不能为0' );
		}
		if (empty ( $opt ['changtype'] )) {
			self::_err ( '变动类型必须填写' );
		}
		if (($opt ['changtype'] == 'ADMIN_ADD' || $opt ['changtype'] == 'ORDER_BAK' || $opt ['changtype'] == 'ADMIN_DECR') && empty ( $opt ['action_userid'] )) {
			self::_err ( '充值、退款操作时操作人ID不能为空。' );
		}
		$opt ['action_userid'] = isset ( $opt ['action_userid'] ) ? $opt ['action_userid'] : $userid;
		$assetc = array (
				'platform' => 'SMW01' 
		);
		$assetc ['changtype'] = $opt ['changtype'];
		$assetc ['action_userid'] = $opt ['action_userid'];
		$assetc ['cip'] = $opt ['cip'];
		$assetc ['bzvu'] = $opt ['bzvu'];
		$assetc ['userid'] = $userid;
		$assetc ['subject'] = $subject;
		$assetc ['ctime'] = time ();
		// 获取用户当前的余额
		$money = self::money_info ( $userid, 'SMW01', $subject );
				
		// 写入用户余额
		if (empty ( $money ) && $money !== false) {
			$money = 0;
		}
		
		if(empty($opt ['oper']) || $opt ['oper']=='+'){
			$money = $money + $value;
			$assetc ['value'] = $value;
		}
		if(!empty($opt ['oper']) && $opt ['oper']=='-'){
			if($money < $value){
				if($isRet==false){
					return false;
				}else{
					return -1;
				}
			}else{
				$money = $money - $value;
				$assetc ['value'] = '-'.$value;
			}
		}		
		// $wres=self::_w_money($userid,$money,'SMW01',$subject);//启用事件回滚的需要暂时不调用此函数
		$where = array ();
		$where ['userid'] = $userid;
		$where ['subject'] = $subject;
		$where ['platform'] = 'SMW01';
		$data = $where;
		$usermoney = M ( 'UserMoney' );
		$res = $usermoney->where ( $where )->find ();
		$usermoney->startTrans ();
		$data ['value'] = $money;
		$data ['mtime'] = time ();
		if ($res) {
			$data ['id'] = $res ['id'];
			$return = $usermoney->save ( $data );
		} else {
			$return = $usermoney->add ( $data );
		}
		if (empty ( $return )) {
			self::_err ( '账号操作错误。' );
		}
		$assetc ['nowvalue'] = $money;
		// 写入充值流水
		$assetc_dao = M ( "UserAssets" );
		$assetc_dao->startTrans ();
		$assetc_id = $assetc_dao->add ( $assetc );
		if ($assetc_id) {
			$usermoney->commit ();
			$assetc_dao->commit ();
			return $money;
		} else {
			$usermoney->rollback ();
			$assetc_dao->rollback ();
			return false;
		}
	} // end recharge
	
	/**
	 * O2O用户充值
	 * 
	 * @param int $userid
	 *        	用户ID
	 * @param string $subject
	 *        	财务科目 CV BV MV PV
	 * @param int $value
	 *        	充值数额 分为单位
	 * @param array $opt=array(
	 *        	'action_userid'=>'操作人',
	 *        	'cip'=>'IP',
	 *        	'bzvu'=>'订单号，授权码，操作原因',
	 *        	'changtype'=>'操作类型 ADMIN_ADD,ORDER_USE,ORDER_BAK,PV_CHANG,MV_CHANG,TENPAY_ADD,CVTOMV,O2oOrderAcceptFreeze,ADMIN_DECR',
	 * 			'oper' => '-'
	 *        	)
	 * @return 当前余额 或 false
	 *         @date 2013-5-17
	 *         @time 下午3:41:31
	 */
	function o2o_recharge($userid = 0, $subject = '', $value = 0, $opt = array(), $isRet = false) {
		if($value>0){
			$value = $value * 100;
		}
		// O2O用户充值
		$this->line = 'o2o_recharge::' . __LINE__;
		if (empty ( $userid )) {
			self::_err ( '用户ID不能为空。' );
		}
		if (empty ( $subject )) {
			self::_err ( '参数错误。' );
		}
		if (empty ( $value ) || $value < 0) {
			self::_err ( '变动数额不能为0' );
		}
		if (empty ( $opt ['changtype'] )) {
			self::_err ( '变动类型必须填写' );
		}
		if (($opt ['changtype'] == 'ADMIN_ADD' || $opt ['changtype'] == 'ORDER_BAK' || $opt ['changtype'] == 'ADMIN_DECR') && empty ( $opt ['action_userid'] )) {
			self::_err ( '充值、退款操作时操作人ID不能为空。' );
		}
		$opt ['action_userid'] = isset ( $opt ['action_userid'] ) ? $opt ['action_userid'] : $userid;
		$assetc = array (
				'platform' => '91YUNGOU' 
		);
		$assetc ['changtype'] = $opt ['changtype'];
		$assetc ['action_userid'] = $opt ['action_userid'];
		$assetc ['cip'] = $opt ['cip'];
		$assetc ['bzvu'] = $opt ['bzvu'];
		$assetc ['userid'] = $userid;
		$assetc ['subject'] = $subject;		
		$assetc ['ctime'] = time ();
		// 获取用户当前的余额
		$money = self::money_info ( $userid, '91YUNGOU', $subject );
		//dump($money);
		// 写入用户余额
		if (empty ( $money ) && $money !== false) {
			$money = 0;
		}
		
		if(empty($opt ['oper']) || $opt ['oper']=='+'){
			$money = $money + $value;
			$assetc ['value'] = $value;
		}
		if(!empty($opt ['oper']) && $opt ['oper']=='-'){
			if($money < $value){
				if($isRet==false){
					return false;
				}else{
					return -1;
				}
			}else{
				$money = $money - $value;
				$assetc ['value'] = '-'.$value;
			}
		}
		//$wres=self::_w_money($userid,$money,'91YUNGOU',$subject);//启用事件回滚的需要暂时不调用此函数
		//_wassets($userid,$action_userid,$platform,$subject,$money,$cip,$bzvu,$changtype)
		//self::_wassets($userid,'91YUNGOU',$subject);
		$where = array ();
		$where ['userid'] = $userid;
		$where ['subject'] = $subject;
		$where ['platform'] = '91YUNGOU';
		$data = $where;
		$usermoney = M ( 'UserMoney' );
		$res = $usermoney->where ( $where )->find ();
		$usermoney->startTrans ();
		$data ['value'] = $money;
		$data ['mtime'] = time ();
		if ($res) {
			$data ['id'] = $res ['id'];
			$return = $usermoney->save ( $data );
		} else {
			$return = $usermoney->add ( $data );
		}
		if (empty ( $return )) {
			self::_err ( '账号操作错误。' );
		}
		$assetc ['nowvalue'] = $money;
		// 写入充值流水
		$assetc_dao = M ( "UserAssets" );
		$assetc_dao->startTrans ();
		$assetc_id = $assetc_dao->add ( $assetc );
		if ($assetc_id) {
			$usermoney->commit ();
			$assetc_dao->commit ();
			return $money;
		} else {
			$usermoney->rollback ();
			$assetc_dao->rollback ();
			return false;
		}
	} // end o2o_recharge
	
	/**
	 * O2O商户给用户充值
	 * 
	 * @param array $userinfo
	 *        	用户信息
	 * @param array $bizinfo
	 *        	商户信息
	 * @param array $opt
	 *        	其它信息
	 *        	@date 2013-5-17
	 *        	@time 下午4:34:51
	 */
	function o2o_biz_user_recharge($userinfo = array(), $bizinfo = array(), $opt = array()) {
		// O2O商户给用户充值
		$this->line = 'o2o_biz_user_recharge::' . __LINE__;
		// 商户扣款
		
		// 用户充值
	} // end o2o_biz_user_recharge
	
	/**
	 * 用户扣款并写入流水
	 * 
	 * @param int $userid
	 *        	用户ID
	 * @param string $subject
	 *        	财务科目 CV BV MV PV
	 * @param int $value
	 *        	充值数额 分为单位
	 * @param array $opt=array(
	 *        	'action_userid'=>'操作人',
	 *        	'cip'=>'IP',
	 *        	'bzvu'=>'操作原因',
	 *        	'changtype'=>'操作类型 BIZ_ADD 此处程序内部固定',
	 *        	)
	 * @return 当前余额 或 false
	 *         @date 2013-5-24
	 *         @time 下午6:21:38
	 */
	function decr($userid = 0, $subject = '', $value = 0, $opt = array()) {
		// 用户扣款
		$this->line = 'decr::' . __LINE__;
		if (empty ( $userid )) {
			self::_err ( '用户ID不能为空。' );
		}
		if (empty ( $subject )) {
			self::_err ( '参数错误。' );
		}
		if (empty ( $value ) || $value < 0) {
			self::_err ( '变动数额不能为0' );
		}
		if (empty ( $opt ['changtype'] )) {
			self::_err ( '变动类型必须填写' );
		}
		if (($opt ['changtype'] == 'ADMIN_DECR') && empty ( $opt ['action_userid'] )) {
			self::_err ( '充值、退款操作时操作人ID不能为空。' );
		}
		$opt ['action_userid'] = isset ( $opt ['action_userid'] ) ? $opt ['action_userid'] : $userid;
		$assetc = array (
				'platform' => 'SMW01' 
		);
		$assetc ['changtype'] = $opt ['changtype'];
		$assetc ['action_userid'] = $opt ['action_userid'];
		$assetc ['cip'] = $opt ['cip'];
		$assetc ['bzvu'] = $opt ['bzvu'];
		$assetc ['userid'] = $userid;
		$assetc ['subject'] = $subject;
		$assetc ['value'] = $value;
		$assetc ['ctime'] = time ();
		// 获取用户当前的余额
		$money = self::money_info ( $userid, 'SMW01', $subject );
		// 写入用户余额
		if (empty ( $money ) && $money !== false) {
			$money = 0;
		}
		if ($money < $value) {
			self::_err ( '余额不足。' );
		}
		$money = $money - $value;
		$where = array ();
		$where ['userid'] = $userid;
		$where ['subject'] = $subject;
		$where ['platform'] = 'SMW01';
		$data = $where;
		$usermoney = M ( 'UserMoney' );
		$res = $usermoney->where ( $where )->find ();
		$usermoney->startTrans ();
		$data ['value'] = $money;
		$data ['mtime'] = time ();
		if ($res) {
			$data ['id'] = $res ['id'];
			$return = $usermoney->save ( $data );
		} else {
			$return = $usermoney->add ( $data );
		}
		if (empty ( $return )) {
			self::_err ( '账号操作错误。' );
		}
		$assetc ['nowvalue'] = $money;
		// 写入充值流水
		$assetc_dao = M ( "UserAssetc" );
		$assetc_dao->startTrans ();
		$assetc_id = $assetc_dao->add ( $assetc );
		if ($assetc_id) {
			$usermoney->commit ();
			$assetc_dao->commit ();
			return $money;
		} else {
			$usermoney->rollback ();
			$assetc_dao->rollback ();
			return false;
		}
	} // end decr
	
	/**
	 * O2O用户扣款
	 * 
	 * @param int $userid
	 *        	用户ID
	 * @param string $subject
	 *        	财务科目 CV BV MV PV
	 * @param int $value
	 *        	充值数额 分为单位
	 * @param array $opt=array(
	 *        	'action_userid'=>'操作人',
	 *        	'cip'=>'IP',
	 *        	'bzvu'=>'操作原因',
	 *        	'changtype'=>'操作类型 BIZ_ADD 此处程序内部固定',
	 *        	)
	 * @return 当前余额 或 false
	 *         @date 2013-5-24
	 *         @time 下午6:23:09
	 */
	function o2o_decr($userid = 0, $subject = '', $value = 0, $opt = array()) {
		// O2O用户扣款
		$this->line = 'o2o_decr::' . __LINE__;
		if (empty ( $userid )) {
			self::_err ( '用户ID不能为空。' );
		}
		if (empty ( $subject )) {
			self::_err ( '参数错误。' );
		}
		if (empty ( $value ) || $value < 0) {
			self::_err ( '变动数额不能为0' );
		}
		if (empty ( $opt ['changtype'] )) {
			self::_err ( '变动类型必须填写' );
		}
		if (($opt ['changtype'] == 'O2O_DECR') && empty ( $opt ['action_userid'] )) {
			self::_err ( '充值、退款操作时操作人ID不能为空。' );
		}
		$assetc = array (
				'platform' => 'O2O' 
		);
		$assetc ['changtype'] = $opt ['changtype'];
		$assetc ['action_userid'] = $opt ['action_userid'];
		$assetc ['cip'] = $opt ['cip'];
		$assetc ['bzvu'] = $opt ['bzvu'];
		$assetc ['userid'] = $userid;
		$assetc ['subject'] = $subject;
		$assetc ['value'] = $value;
		$assetc ['ctime'] = time ();
		// 获取用户当前的余额
		$money = self::money_info ( $userid, 'O2O', $subject );
		// 写入用户余额
		if (empty ( $money ) && $money !== false) {
			$money = 0;
		}
		if ($money < $value) {
			self::_err ( '余额不足。' );
		}
		$money = $money - $value;
		
		$where = array ();
		$where ['userid'] = $userid;
		$where ['subject'] = $subject;
		$where ['platform'] = 'O2O';
		$data = $where;
		$usermoney = M ( 'UserMoney' );
		$res = $usermoney->where ( $where )->find ();
		$usermoney->startTrans ();
		$data ['value'] = $money;
		$data ['mtime'] = time ();
		if ($res) {
			$data ['id'] = $res ['id'];
			$return = $usermoney->save ( $data );
		} else {
			$return = $usermoney->add ( $data );
		}
		if (empty ( $return )) {
			self::_err ( '账号操作错误。' );
		}
		$assetc ['nowvalue'] = $money;
		// 写入充值流水
		$assetc_dao = M ( "UserAssetc" );
		$assetc_dao->startTrans ();
		$assetc_id = $assetc_dao->add ( $assetc );
		if ($assetc_id) {
			$usermoney->commit ();
			$assetc_dao->commit ();
			return $money;
		} else {
			$usermoney->rollback ();
			$assetc_dao->rollback ();
			return false;
		}
	} // end o2o_decr
	
	/**
	 * 记录用户分期
	 * 
	 * @param array $arr=array('userid'=>'','subject'=>'CV','value'=>'120000','content'=>'分期原因')        	
	 * @param string $platform='SMW01'
	 *        	分期的平台
	 * @param int $num=12
	 *        	分期数
	 * @param time $starttime=0
	 *        	分期开始时间 0为现在（为0时，会发放第一期）<br>
	 *        	@date 2013-5-20
	 *        	@time 下午5:16:53
	 */
	function stages($arr = array(), $platform = 'SMW01', $num = 12, $starttime = 0) {
		// 记录用户分期
		$this->line = 'stages::' . __LINE__;
		;
	} // end stages
	
	/**
	 * 记录一次分期记录
	 * @date 2013-5-21
	 * @time 下午4:44:29
	 */
	function stages_log() {
		// 记录一次分期记录
		$this->line = 'stages_log::' . __LINE__;
	} // end stages_log
	
	/**
	 * 修改用户财务信息
	 * @date 2013-5-24
	 * @time 下午3:08:40
	 */
	protected function _w_money($userid = 0, $value = 0, $platform = 'SMW01', $subject = 'CV') {
		// 修改用户财务信息
		$this->line = 'w_money::' . __LINE__;
		$where = array ();
		$where ['userid'] = $userid;
		$where ['subject'] = $subject;
		$where ['platform'] = $platform;
		$data = $where;
		$dao = M ( 'Money' );
		$res = $dao->where ( $where )->find ();
		$data ['value'] = $value;
		$data ['mtime'] = time ();
		if ($res) {
			$data ['id'] = $res ['id'];
			$return = $dao->save ( $data );
		} else {
			$return = $dao->add ( $data );
		}
		if ($return === false) {
			return false;
		} else {
			return true;
		}
	} // end w_money
	
	/**
	 * 获取用户余额
	 * 
	 * @param int $userid
	 *        	用户ID
	 * @param string $platform
	 *        	平台信息 SMW01 O2O
	 * @param string $subject
	 *        	财务科目 CV BV MV PV MONEY
	 *        	@date 2013-5-23
	 *        	@time 下午5:58:41
	 */
	function money_info($userid = 0, $platform = 'SMW01', $subject = 'all') {
		// 获取用户余额
		$this->line = 'money_info::' . __LINE__;
		$user = M ( "UserMoney" );
		$where = array ();
		if ($platform != 'all') {
			$where ['platform'] = $platform;
		}
		$where ['userid'] = $userid;
		$info = $user->where ( $where )->select ();
		if (empty ( $info )) {
			return false;
		}
		$res = array ();
		foreach ( $info as $v ) {
			//dump($info);
			$res [$v ['platform']] [$v ['subject']] = $v ['value'];
		}
		if ($platform != 'all') {
			if ($subject != 'all') {
				return $res [$platform] [$subject];
			} else {
				return $res [$platform];
			}
		}
		return $res;
	} // end money_info
	
	/**
	 * 写入用户财务流水
	 * @param 用户ID  $userid
	 * @param 操作用户ID  $action_userid
	 * @param 平台标示        $platform
	 * @param 帐户科目      $subject
	 * @param 变动金额     $money
	 * @param 当前IP  $cip
	 * @param 金额备注    $bzvu
	 * @param 变动类型    $changtype
	 * @return boolean
	 */
	protected function _wassets($userid,$action_userid,$platform,$subject,$money,$cip,$bzvu,$changtype) {
		if(empty($userid)){
			return false;
		}else{
			$dao=M('UserAssets');
			$usermoney=M("UserMoney");
			$where = array();
			$where ['userid'] = $userid;
			$where ['subject'] = $subject;
			$where ['platform'] = $platform;
			$usermoney=$usermoney->field('id,value')->where($where)->find();
			if($usermoney){
				$data=array();
				$data['userid']=$userid;
				$data['action_userid']=$action_userid;
				$data['platform']=$platform;
				$data['subject']=$subject;
				$data['value']=$money;
				$data['nowvalue']=$usermoney['value'];
				$data['ctime']=time();
				$data['cip']=$cip;
				$data['bzvu']=$bzvu;
				$data['changtype']=$changtype;
				$uaid=$dao->add($data);
				if($uaid){
					return true;
				}else{
					return false;
				}
			}else{
				return false;  
			}
		}
	}
	/**
	 * 分期到账列表
	 * */
	function give($userids = array(), $limit = '0,20') {
		// 用户分期到账列表
		$dao = M ( "UserGive" );
		$where = array ();
		$ids = array ();
		if (is_array ( $userids )) {
			foreach ( $userids as $v ) {
				$ids [] = $v ['id'];
			}
		} else {
			$ids [] = $userids;
		}
		$where ['userid'] = array (
				'IN',
				implode ( ',', $ids ) 
		);
		
		$res = array ();
		if ($limit == 'all') {
			return $dao->where ( $where )->count ();
		}
		$data = $dao->where ( $where )->order ( "createtime DESC" )->limit ( $limit )->select ();
		$temp=array();
		foreach ($data as $v) {
			$temp[$v['userid']][]=$v;
		}
		if (is_array ( $userids )) {
			$res = $temp;
		} else {
			$res = $temp [$userids];
		}
		return $res;
	} // end give 
	/**
	 * 优惠劵列表
	 * */
	function coupon($userids = array(), $limit = '0,20') {
		// 用户优惠劵列表
		$dao = M ( "UserCoupon" );
		$where = array ();
		$ids = array ();
		if (is_array ( $userids )) {
			foreach ( $userids as $v ) {
				$ids [] = $v ['id'];
			}
		} else {
			$ids [] = $userids;
		}
		$where ['userid'] = array (
				'IN',
				implode ( ',', $ids ) 
		);
		
		$res = array ();
		if ($limit == 'all') {
			return $dao->where ( $where )->count ();
		}
		$data = $dao->where ( $where )->order ( "createtime DESC" )->limit ( $limit )->select ();
		$temp=array();
		foreach ($data as $v) {
			$temp[$v['userid']][]=$v;
		}
		if (is_array ( $userids )) {
			$res = $temp;
		} else {
			$res = $temp [$userids];
		}
		return $res;
	} // end coupon 
	/**
	 * 用户转帐充值留言列表
	 * */
	function pay_leaveword($userids = array(), $limit = '0,20') {
		// 用户转帐充值留言表
		$dao = M ( "UserBankPay" );
		$where = array ();
		$ids = array ();
		if (is_array ( $userids )) {
			foreach ( $userids as $v ) {
				$ids [] = $v ['id'];
			}
		} else {
			$ids [] = $userids;
		}
		if ( $userids ) {
			$where ['userid'] = array (
					'IN',
					implode ( ',', $ids ) 
			);
		}
		
		$res = array ();
		if ($limit == 'all') {
			return $dao->where ( $where )->count ();
		}
		$data = $dao->where ( $where )->order ( "createtime DESC" )->limit ( $limit )->select ();
		$temp=array();
		foreach ($data as $v) {
			$temp[$v['userid']][]=$v;
		}
		if (is_array ( $userids )) {
			$res = $temp;
		} else {
			$res = $temp [$userids];
		}
		return $res;
	} // end payleaveword 
	
	
	/**
	 * 用户财务流水
	 *
	 * @param array $userids
	 *        	用户ID数组
	 *        	@date 2013-6-19
	 *        	@time 下午3:30:30
	 */
	function get_assets($limit = '0,20',$where=array()) {
		// 用户财务流水
		$dao = M ( "UserAssets" );

		$res = array ();
		if ($limit == 'all') {
			return $dao->where ( $where )->count ();
		}
		$data = $dao->where ( $where )->order ( "id DESC" )->limit ( $limit )->select ();print_r($data);
		$temp=array();
		foreach ($data as $v) {
			$temp[$v['userid']][]=$v;
		}
		if (is_array ( $userids )) {
			$res = $temp;
		} else {
			$res = $temp [$userids];
		}
		return $res;
	} // end assets
	
	/**
	 * 用户财务流水
	 * 
	 * @param array $userids
	 *        	用户ID数组
	 *        	@date 2013-6-19
	 *        	@time 下午3:30:30
	 *        @param $where 其他条件
	 */
	function assets($userids = array(), $platform = 'SMW01', $limit = '0,20', $subject = '',$changtype='',$where=array()) {
		// 用户财务流水
		$dao = M ( "UserAssets" );
		$where ['platform'] = $platform;
		$ids = array ();
		if (is_array ( $userids )) {
			foreach ( $userids as $v ) {
				$ids [] = $v ['id'];
			}
		} else {
			$ids [] = $userids;
		}
		$where ['userid'] = array (
				'IN',
				implode ( ',', $ids ) 
		);
		if ($subject) {
			$where ['subject'] = array (
					'IN',
					$subject 
			);
		}
		if ($changtype) {
			$where ['changtype'] = array (
					'IN',
					$changtype 
			);
		}
		$res = array ();
		if ($limit == 'all') {
			 $count = $dao->where ( $where )->count ();
			 return $count;
		}
		$data = $dao->where ( $where )->order ( "id DESC" )->limit ( $limit )->select ();
		$temp=array();
		foreach ($data as $v) {
			$temp[$v['userid']][]=$v;
		}
		if (is_array ( $userids )) {
			$res = $temp;
		} else {
			$res = $temp [$userids];
		}
		return $res;
	} // end assets
	
	/**
	 * 输出错误信息
	 * @date 2013-5-6
	 * @time 下午5:19:24
	 */
	function _err($arr = array(), $str = "暂无相关信息") {
		// 请求接口类型异常
		if ($this->isdebug == 1) {
			echo '<br>输出的数据:<br><textarea rows="15" cols="120">';
		}
		if (is_array ( $arr ) && ! empty ( $arr )) {
			$res = array (
					"status" => 0,
					"msg" => $str,
					'data' => $arr,
					'line' => $this->line 
			);
		} else {
			$res = array (
					"status" => 0,
					"msg" => $str,
					'data' => $this->debug,
					'line' => $this->line 
			);
		}
		echo self::myjson ( $res );
		if ($this->isdebug == 1) {
			echo '</textarea>';
		}
		die ();
	} // end _err
	
	/**
	 * 请求成功返回
	 * @date 2011-11-15
	 * @time 上午08:10:19
	 */
	private function _suc($arr = array(), $str = "操作成功") {
		// 请求接口类型异常
		$res = array (
				"status" => 1,
				"msg" => $str,
				'data' => $this->debug 
		);
		if (! empty ( $arr )) {
			$res = array_merge ( $res, $arr );
		}
		if ($this->isdebug == 1) {
			echo '<br>输出的数据:<br><textarea rows="15" cols="120">';
		}
		echo self::myjson ( $res );
		if ($this->isdebug == 1) {
			echo '</textarea>';
		}
		die ();
	} // end _suc
	private function myjson($code) {
		$code = json_encode ( $this->urlencodeAry ( $code ) );
		return urldecode ( $code );
		//return $code;
	}
}//end Money